package com.woniu.adapter;

import com.woniu.outlet.client.WorkingProcedures;
import com.woniu.outlet.dao.RepairAcceptanceMapper;
import com.woniu.outlet.dao.RepairApplicationMapper;
import com.woniu.outlet.po.*;
import com.woniu.service.EmailService;
import com.woniu.util.ResponseResult;
import com.woniu.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Component
public class RepairAdapter {
    @Autowired
    private RepairApplicationMapper repairApplicationMapper;
    @Autowired
    private RepairAcceptanceMapper repairAcceptanceMapper;
    @Autowired
    private JavaMailSenderImpl mailSender;
    @Autowired
    private WorkingProcedures workingProcedures;

    public void addRepairApplication(RepairApplication repairApplication){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("3088896870@qq.com");
        Integer procedureId = repairApplication.getProcedureId();
        ResponseResult<WorkerVo> workerById = null;
        try {
             workerById= workingProcedures.getWorkerById(procedureId.longValue());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String email = workerById.getData().getEmail();
        message.setTo(email);
        message.setSubject("上海秋怡电子厂ERP返修申请");
        message.setText(repairApplication.getOrderCode()+" 订单的申请返修,数量为"+repairApplication.getRepairNum()+",返修工序为"+repairApplication.getProcedureName());
        mailSender.send(message);
        repairApplication.setCheck("未审核");
        repairApplication.setSpare1(TimeUtil.getNowTime(new Date()));
        repairApplicationMapper.insertSelective(repairApplication);
        workingProcedures.addSystemLog("添加返修申请");
    }

    public List<RepairApplication> getRepairApplicationList(String orderCode){
        RepairApplicationExample repairApplicationExample = new RepairApplicationExample();
        RepairApplicationExample.Criteria criteria = repairApplicationExample.createCriteria();
        if(!StringUtils.isEmpty(orderCode)){
            criteria.andOrderCodeLike("%"+orderCode+"%");
        }
        criteria.andCheckEqualTo("未审核");
        List<RepairApplication> repairApplications = repairApplicationMapper.selectByExample(repairApplicationExample);
        workingProcedures.addSystemLog("查询所有未审核的返修申请");
        return repairApplications;
    }

    public void updateStatus(Integer id,boolean flag,String remarks){
        RepairApplication repairApplication = new RepairApplication();
        repairApplication.setId(id);
        if(flag){
            repairApplication.setCheck("已审核");
        }else{
            repairApplication.setCheck("审核不通过");
        }
        if(StringUtils.isEmpty(remarks)){
            repairApplication.setRemarks(remarks);
        }
        repairApplication.setSpare1(TimeUtil.getNowTime(new Date()));
        workingProcedures.addSystemLog("返修审核");
        repairApplicationMapper.updateByPrimaryKeySelective(repairApplication);
    }

    public List<RepairApplication> getRepairList(String orderCode){
        RepairApplicationExample repairApplicationExample = new RepairApplicationExample();
        RepairApplicationExample.Criteria criteria = repairApplicationExample.createCriteria();
        if(!StringUtils.isEmpty(orderCode)){
            criteria.andOrderCodeLike("%"+orderCode+"%");
        }
        criteria.andCheckEqualTo("已审核");
        List<RepairApplication> repairApplications = repairApplicationMapper.selectByExample(repairApplicationExample);
        workingProcedures.addSystemLog("获取所有已审核的返修申请");
        return repairApplications;
    }

    public void product(Integer id){
        RepairApplication repairApplication = new RepairApplication();
        repairApplication.setId(id);
        repairApplication.setCheck("生产中");
        repairApplication.setSpare1(TimeUtil.getNowTime(new Date()));
        repairApplicationMapper.updateByPrimaryKeySelective(repairApplication);
        workingProcedures.addSystemLog("生产项目");
    }

    public List<RepairApplication> getCheck(String orderCode){
        RepairApplicationExample repairApplicationExample = new RepairApplicationExample();
        RepairApplicationExample.Criteria criteria = repairApplicationExample.createCriteria();
        if(!StringUtils.isEmpty(orderCode)){
            criteria.andOrderCodeLike("%"+orderCode+"%");
        }
        criteria.andCheckEqualTo("已验收");
        List<RepairApplication> repairApplications = repairApplicationMapper.selectByExample(repairApplicationExample);
        workingProcedures.addSystemLog("查询所有已验收的");
        return repairApplications;
    }

    public void check(RepairAcceptance repairAcceptance){
        RepairApplication repairApplication = new RepairApplication();
        repairApplication.setId(repairAcceptance.getRepairApplicationId());
        repairApplication.setCheck("已验收");
        repairApplication.setSpare1(TimeUtil.getNowTime(new Date()));
        repairApplicationMapper.updateByPrimaryKeySelective(repairApplication);
        repairAcceptance.setSpare1(TimeUtil.getNowTime(new Date()));
        repairAcceptanceMapper.insertSelective(repairAcceptance);
        workingProcedures.addSystemLog("返修验收");
    }


    public RepairApplication getRepairApplicationDetail(Integer id){
        RepairApplication repairApplication = repairApplicationMapper.selectByPrimaryKey(id);
        RepairAcceptanceExample repairAcceptanceExample = new RepairAcceptanceExample();
        RepairAcceptanceExample.Criteria criteria = repairAcceptanceExample.createCriteria();
        criteria.andRepairApplicationIdEqualTo(id);
        List<RepairAcceptance> repairAcceptances = repairAcceptanceMapper.selectByExample(repairAcceptanceExample);
        repairApplication.setRepairAcceptance(repairAcceptances.get(0));
        workingProcedures.addSystemLog("查询返修申请详情");
        return repairApplication;
    }

    public void producted (Integer id) {
        RepairApplication repairApplication = new RepairApplication();
        repairApplication.setId(id);
        repairApplication.setCheck("未验收");
        repairApplication.setSpare1(TimeUtil.getNowTime(new Date()));
        repairApplicationMapper.updateByPrimaryKeySelective(repairApplication);
        workingProcedures.addSystemLog("返修生产以完成");
    }

    public List<RepairApplication> getProducted(String orderCode){
        RepairApplicationExample repairApplicationExample = new RepairApplicationExample();
        RepairApplicationExample.Criteria criteria = repairApplicationExample.createCriteria();
        if(!StringUtils.isEmpty(orderCode)){
            criteria.andOrderCodeLike("%"+orderCode+"%");
        }
        criteria.andCheckEqualTo("未验收");
        List<RepairApplication> repairApplications = repairApplicationMapper.selectByExample(repairApplicationExample);
        workingProcedures.addSystemLog("查询所有返修未验收的");
        return repairApplications;
    }
}
